package edu.jit.flowers.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.jit.flowers.service.UsersService;import edu.jit.flowers.bean.Users;
import edu.jit.flowers.utils.BaseServlet;

public class UsersServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private UsersService uService = new UsersService();

	public String loginCheck(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Users u = uService.loginCheck(username, password);
		String msg = null;
		if (u == null) {
			msg = "登录失败，用户名或密码有误";
			String uri = null;
			try {
				uri = "redirect:userLogin.jsp?msg=" + URLEncoder.encode(msg, "utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return uri;
		} else {
			// 用户界面
			int role = u.getuRole();
			if (role == 0) {
				request.getSession().setAttribute("username", u.getuName());
				return "redirect:header.jsp";
			} else {
				// 管理员界面
				request.getSession().setAttribute("username", u.getuName());
				return "redirect:backJsp/homeframe.jsp";
			}
		}
	}

	public String registerUser(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String realname = request.getParameter("realname");
		int age = Integer.parseInt(request.getParameter("age").trim());
		String tel = request.getParameter("tel");
		String gender = request.getParameter("gender");
		String address = request.getParameter("address");
		String password = request.getParameter("password");
		String password1 = request.getParameter("password1");
		if (password.equals(password1)) {
			uService.addUsers(new Users(0, username, realname, age, tel, gender, password, 0, address));
			return "redirect:userLogin.jsp";
		} else {
			return "redirect:register.jsp";
		}
	}

	public String getAllUsers(HttpServletRequest request, HttpServletResponse response) {
		List<Users> users = uService.getAllCommonsByRole();
		request.setAttribute("users", users);
		return "forward:backJsp/usersShow.jsp";
		// usersShow.jsp
	}

	public String addUser(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String realname = request.getParameter("realname");
		int age = Integer.parseInt(request.getParameter("age").trim());
		String tel = request.getParameter("tel");
		String gender = request.getParameter("gender");
		String address = request.getParameter("address");
		String password = request.getParameter("password");
		uService.addUsers(new Users(0, username, realname, age, tel, gender, password, 0, address));
		return "redirect:Users?method=getAllUsers";
	}

	public String prepareUpdateView(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		Users u = uService.getAllCommonsByUName(username);
		request.setAttribute("users", u);
		return "forward:updateStudent.jsp";
	}

	public String updateUsersByUName(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		String realname = request.getParameter("realname");
		int age = Integer.parseInt(request.getParameter("age").trim());
		String tel = request.getParameter("tel");
		String gender = request.getParameter("gender");
		String address = request.getParameter("address");
		String password = request.getParameter("password");
		//System.out.println(tel);
		uService.updateUsers(new Users(0, username, realname, age, tel, gender, password, 0, address));
		return "redirect:Users?method=getAllUsers";
	}

	public String invalidateSession(HttpServletRequest request, HttpServletResponse response) {
		request.getSession().removeAttribute("username");
		request.getSession().invalidate();
		return "redirect:index.jsp";
	}
}
